package com.qitianyong.selfclass;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.ligo.log.LogModule;
import com.qitianyong.sgkusbcamera.BuildConfig;
import com.tencent.bugly.crashreport.CrashReport;
import com.zhy.base.cache.disk.LruCacheHelper;

import org.greenrobot.eventbus.EventBus;


import java.io.IOException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import cn.onlinecache.breakpad.NativeBreakpad;


/**
 * Created by gy on 2017/4/5.
 */

public class CustomApp extends Application {
    public static LruCacheHelper __lruCacheHelper;
    public static CustomApp customApp;
    public static Lock __lock = new ReentrantLock();
    public static String usbDiskPath = "";

    @Override
    public void onCreate() {
        super.onCreate();
//        Timber.plant(new Timber.DebugTree(){
//            @Override
//            protected void log(int priority, String tag, @NotNull String message, Throwable t) {
//                super.log(priority, tag, message, t);
//            }
//        });
        LogModule.initial(this, BuildConfig.APPLICATION_ID, BuildConfig.VERSION_NAME);
        LogModule.setLogEnable(true);
        LogModule.getInstance().setSaveLog(true);
        LogUtils.ids.size();
        customApp = this;
        try {
            __lruCacheHelper = new LruCacheHelper(this);
        } catch (IOException e) {
            e.printStackTrace();
            __lruCacheHelper = null;
        }
        registerActivityLifecycleCallbacks(activityLifecycleCallbacks);
        NativeBreakpad.getInstance().init(this);
        CrashReport.initCrashReport(getApplicationContext(), "2f82cc4b46", false);
    }

    private ActivityLifecycleCallbacks activityLifecycleCallbacks = new ActivityLifecycleCallbacks() {
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            LogUtils.addLog(activity.getComponentName().getShortClassName() + " onActivityCreated");
        }

        @Override
        public void onActivityStarted(Activity activity) {

        }

        @Override
        public void onActivityResumed(Activity activity) {
            LogUtils.addLog(activity.getComponentName().getShortClassName() + " onActivityResumed");
        }

        @Override
        public void onActivityPaused(Activity activity) {

        }

        @Override
        public void onActivityStopped(Activity activity) {
            LogUtils.addLog(activity.getComponentName().getShortClassName() + " onActivityStopped");

            Message obtain = Message.obtain();
            obtain.what = LogUtils.SAVE_LOG;
            LogUtils.handler.sendMessage(obtain);
        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {
            LogUtils.addLog(activity.getComponentName().getShortClassName() + " onActivityDestroyed");
        }
    };
    public static Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            EventBus.getDefault().post(new BaseActivityEvent(BaseActivityEvent.OPENSTREAM));
        }
    };

}
